package com.mandala.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.mandala.LDAPConnectService;
import com.mandala.entry.ResponseWrapper;
import com.mandala.entry.Role;
import com.mandala.entry.User;
import com.mandala.service.JsonResult;
import com.mandala.service.OdmUserRepo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/role")
@Api("角色查询接口")
public class RoleController {
	@Autowired
	private OdmUserRepo odmUserRepo;
	@Autowired
	private JsonResult jsonResult;
	@Autowired
	private LDAPConnectService connectService;
	
    @ApiOperation(value = "根据角色CN", notes = "角色查询")
    @ApiImplicitParam(name = "cn", value = "机构ID", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getRoleByCn/{cn}" , method = RequestMethod.GET)
    public  ResponseWrapper getRoleByCn(@PathVariable("cn") String cn) {
    	ResponseWrapper response = new ResponseWrapper();
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	if(cn.equals("null") || cn ==null){
    		return jsonResult.markParamError();
    	}
    	Role role = new Role();
    	role = odmUserRepo.findRoleByCn(cn);
		if(role != null){
			response = jsonResult.markSuccess(role);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
    }
    
    /*@ApiOperation(value="获取所有角色列表", notes="获取所有角色列表")
    @RequestMapping(value={""}, method=RequestMethod.GET)
    public List<Role> getRoleList() {
    	List<Role> r = odmUserRepo.findAllRole();
        return r;
     }*/
    
    @ApiOperation(value="根据用户工号", notes="用户所有角色查询")
    @ApiImplicitParam(name = "user", value = "用户工号", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value={"/getRoleByUserCode/{user}"}, method=RequestMethod.GET)
    public ResponseWrapper getRoleByUserCode(@PathVariable("user") String userCode) {
    	ResponseWrapper response = new ResponseWrapper();
    	if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	//如果传入的是空值，就返回传入的参数有误
    	if(userCode.equals("null") || userCode ==null){
    		return jsonResult.markParamError();
    	}
    	//通过用户工号查询用户信息
    	User user = odmUserRepo.findUserByUid(userCode);
    	//根据用户信息查询该用户绑定的动态角色，静态角色和系统角色
    	if(user != null){
    		List<Role> list = odmUserRepo.findRoleByConditions(user);
        	if(list.size()>0){
    			response = jsonResult.markSuccess(list);
            }else{
            	response = jsonResult.markSuccessButNoData();
            }
    	}else{
    		response = jsonResult.markSuccessButNoData();
    	}
    	
        return response;
     }
    
    
}